package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Permission;
import cn.wolfcode.mapper.PermissionMapper;
import cn.wolfcode.qo.PageResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service
public class PermissionServiceImpl implements IPermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public void save(Permission permission) {
        permissionMapper.insert(permission);
    }

    @Override
    public void delete(Long id) {
        permissionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Permission permission) {
        permissionMapper.updateByPrimaryKey(permission);
    }

    @Override
    public Permission get(Long id) {
        return permissionMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Permission> listAll() {
        return permissionMapper.selectAll();
    }

    @Override
    public PageInfo<Permission> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Permission> permissions = permissionMapper.selectForList(qo);
        return new PageInfo(permissions);

    }

    @Autowired
    private ApplicationContext ctx;

    @Override
    public void reload() {

        // 查询全部权限表达式
        List<String> expressions =  permissionMapper.selectAllExpression();

        // 1. 从 Spring 容器中 回去所有的 Controller
        Map<String, Object> beans = ctx.getBeansWithAnnotation(Controller.class);
        // 2. 获取所有的 value 值
        Collection<Object> values = beans.values();
        // 3. 遍历循环每一个 Controller
        for (Object controller:values){
            // 获取controller 的字节码对象
            Class clazz = controller.getClass();
            // 4. 获取 Controller 的所有方法
            Method[] mothods = clazz.getDeclaredMethods();
            // 5. 遍历每一个方法 判断是否有贴自定义权限注解
            for (Method method:mothods){
                RequiredPermission annotation = method.getAnnotation(RequiredPermission.class);
                // 6. 如果贴了 封装成权限对象 存到数据库
                if(annotation != null){
                    // 先获取条件表达式的英文
                    String expression = annotation.expression();
                    // 7. 判断数据库是否已经存在 不存在才添加
                    if(!expressions.contains(expression)){
                        Permission permission = new Permission();
                        permission.setName(annotation.name());
                        permission.setExpression(expression);
                        permissionMapper.insert(permission);
                    }
                }


            }

        }






    }

    @Override
    public List<String> selectByEmployeeId(Long id) {
        return permissionMapper.selectByEmployeeId(id);
    }
}
